package org.maverickjs.web.servlet;

import javax.persistence.EntityManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

import org.maverick.persistence.DAO;
import org.maverick.persistence.simple.jpa.Persistence;

public class ServletBase extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public ServletBase() {
		super();
	}

	@SuppressWarnings({ "rawtypes" })
	protected DAO createDAO(HttpServletRequest req, Class daoClass) {

		try {
			EntityManager em = (EntityManager) req.getAttribute("default");
			Object dao = (DAO) daoClass.newInstance();
			((Persistence) dao).injectEntityManager(em);
			return (DAO) dao;
		} catch (Throwable e) {

			e.printStackTrace();

			return null;
		}
	}

}
